#include <stdint.h>
#include <stdio.h>
#include "driver/gpio.h"
#include "esp_err.h"
#include "esp_mac.h"
#include "esp_log.h"

#include "rtc_wdt.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_task_wdt.h"
#include "hal/gpio_types.h"
static esp_task_wdt_user_handle_t func_a_twdt_user_hdl;
uint8_t num_table[10] = {
    0x3F, // 0
    0x06, // 1
    0x5B, // 2
    0x4F, // 3
    0x66, // 4
    0x6D, // 5
    0x7D, // 6
    0x07, // 7
    0x7F, // 8
    0x6F, // 9
};

#define PIN1  GPIO_NUM_4
#define PIN2  GPIO_NUM_5
#define PIN3  GPIO_NUM_6
#define PIN4  GPIO_NUM_7
#define PIN5  GPIO_NUM_8

#define A3 (1 << 0)
#define B3 (1 << 1)
#define C3 (1 << 2)
#define D3 (1 << 3)
#define E3 (1 << 4)
#define F3 (1 << 5)
#define G3 (1 << 6)

#define A2 (1 << 7)
#define B2 (1 << 8)
#define C2 (1 << 9)
#define D2 (1 << 10)
#define E2 (1 << 11)
#define F2 (1 << 12)
#define G2 (1 << 13)

#define B1 (1 << 14)
#define C1 (1 << 15)
#define K1 (1 << 16)
#define K2 (1 << 17)
//                        //00 00 0000000 0000000
uint32_t current_state = 0b000000000000000000;
void print18BitBinary(unsigned int num) {
    // 确保只取18位（0-262143）
    num &= 0x3FFFF; // 0x3FFFF = 二进制 11 1111 1111 1111 1111
    
    printf("18位二进制: ");
    for (int i = 17; i >= 0; i--) {
        printf("%d", (num >> i) & 1);
        if (i % 4 == 0 && i != 0) printf(" "); // 每4位加空格
    }
    printf("\n");
}
extern esp_err_t gpio_output_disable(gpio_num_t gpio_num);
extern esp_err_t gpio_output_enable(gpio_num_t gpio_num);
// 将引脚重新配置为输入模式
void led188_reset(){
   /*   */
    gpio_output_disable(PIN1);
    gpio_output_disable(PIN2);
    gpio_output_disable(PIN3);
    gpio_output_disable(PIN4);
    gpio_output_disable(PIN5);
    /* 
    gpio_set_direction(PIN1, GPIO_MODE_INPUT);
    gpio_set_direction(PIN2, GPIO_MODE_INPUT);
    gpio_set_direction(PIN3, GPIO_MODE_INPUT);
    gpio_set_direction(PIN4, GPIO_MODE_INPUT);
    gpio_set_direction(PIN5, GPIO_MODE_INPUT);*/
    
    /*
    gpio_set_level(PIN1,0);
    gpio_set_level(PIN2,0);
    gpio_set_level(PIN3,0);
    gpio_set_level(PIN4,0);
    gpio_set_level(PIN5,0);*/
    
}
void led188_set(uint8_t pin, uint8_t level){
     
    //gpio_set_direction(pin, GPIO_MODE_OUTPUT);
    
    gpio_output_enable(pin);
    gpio_set_level(pin, level);
    //esp_task_wdt_reset_user(func_a_twdt_user_hdl);
}

void led188_show_num(uint8_t num){
    if(num>100){
        num = 100;
    }
    if(num == 100){
        current_state &= ~(0xFFFF);
        current_state |= 0b11<<14|num_table[0]<<7|num_table[0];
    }else{
        // 清除百位
        current_state &= ~0xC000;
        // 取出十位
        int shi = num/10;
        // 清楚原来十位上的数状态 0011 1111 1000 0000
        current_state &= ~(0x3F80);//~(0x7F<<7);
        if(shi!=0){
            current_state |= num_table[shi]<<7;
        }
        // 取出个位
        int ge = num%10;
        current_state &= ~(0x7F);
        current_state |= num_table[ge];
    }
}

void led188_display_task(){

    uint8_t i = 0;
    //ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
    //ESP_ERROR_CHECK(esp_task_wdt_status(NULL));
    
    while(1){
        extern int current_ec12d_value;
        int show_num = current_ec12d_value;
        if(show_num<0){
            show_num = 0;
        }

        led188_show_num(show_num);
        led188_reset();
       
        switch (i) {
            case 1:
                led188_set(PIN1, 1);
               

                if(current_state&A3) led188_set(PIN2, 0);
                if(current_state&C3) led188_set(PIN3, 0);
                if(current_state&E3) led188_set(PIN4, 0);
                
                break;
            case 2:
                led188_set(PIN2, 1);



                if(current_state&B3) led188_set(PIN1, 0);
                if(current_state&A2) led188_set(PIN3, 0);
                if(current_state&C1) led188_set(PIN4, 0);
                if(current_state&K2) led188_set(PIN5, 0);
                
                break;
            case 3:
                led188_set(PIN3, 1);
                if(current_state&D3) led188_set(PIN1, 0);
                if(current_state&B2) led188_set(PIN2, 0);
                if(current_state&B1) led188_set(PIN4, 0);
                if(current_state&K1) led188_set(PIN5, 0);
                
                break;
            case 4:
                led188_set(PIN4, 1);
                if(current_state&F3) led188_set(PIN1, 0);
                if(current_state&D2) led188_set(PIN2, 0);
                if(current_state&C2) led188_set(PIN3, 0);
               
                //gpio_set_direction(PIN5, GPIO_MODE_INPUT);
                break;
            case 5:
                led188_set(PIN5, 1);
                if(current_state&G3) led188_set(PIN1, 0);
                if(current_state&E2) led188_set(PIN2, 0);
                if(current_state&F2) led188_set(PIN3, 0);
                if(current_state&G2) led188_set(PIN4, 0);
                
                i = 0;
                break;
        }
        
        //esp_task_wdt_reset();
        //esp_task_wdt_reset_user(func_a_twdt_user_hdl);
        vTaskDelay(pdMS_TO_TICKS(5));
        //vTaskDelayUntil(&xLastWakeTime, xFrequency);
        i++;
    }
}

void example_LED188_test_main(){
    printf("LED188测试开始\r\n");
    esp_log_level_set("gpio", ESP_LOG_ERROR);
    gpio_config_t bk_gpio_config = {
        .mode = GPIO_MODE_INPUT_OUTPUT,
        .pin_bit_mask = 1ULL << PIN1 | 1ULL << PIN2 | 1ULL << PIN3 | 1ULL << PIN4 | 1ULL << PIN5,
        .pull_up_en = GPIO_PULLUP_DISABLE,
        .pull_down_en = GPIO_PULLDOWN_DISABLE,
        .intr_type = GPIO_INTR_DISABLE
    };
    ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));


    //ESP_ERROR_CHECK(esp_task_wdt_add_user("func_a", &func_a_twdt_user_hdl));


    // gpio初始化
    led188_reset();
    //led188_show_num(88);
    xTaskCreate(led188_display_task, "LED188_DISPLAY", 4096, NULL, 3, NULL);
}
